#!/usr/bin/env jq
# compile-config-0.51-add_init_app -- Adds an extractor for initialzing the application
##
.deepdive_ as $deepdive

# add a database initialization process
| .deepdive_.execution.processes +=
    # NOTE when process_init_app is overridden, compiler does not define the process and assume it points to an already defined process
    if $deepdive.execution.process_init_app == "process/init/app" then
    ([{ key: $deepdive.execution.process_init_app, value:
        { style: "cmd_extractor"
        , cmd: (
        # allow overriding app initialization steps
        (try .deepdive_.execution.commands.init_app catch null) // "
        cd \"$DEEPDIVE_APP\"

        # run a top-level init executable
        ! [[ -x init ]] || ./init

        deepdive db init

        # run legacy schema.sql
        if [[ -r schema.sql ]]; then
            deepdive db prompt <schema.sql
        fi
        # run legacy init script
        if [[ -x input/init.sh ]]; then
            input/init.sh
        fi
        ") }
    }] | from_entries)
    else {} end
